home *** CD-ROM | disk | FTP | other *** search
/ PC PowerPlay 58 / pcpp58a.iso / extras / quake 3 source / Q3A_ToolSource.exe / Main / jpgload.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-02  |  4.9 KB  |  143 lines

  1.  
  2.     
  3. #include "jpeglib.h"
  4. #include <memory.h>
  5.  
  6. GLOBAL void LoadJPGBuff(unsigned char *fbuffer, unsigned char **pic, int *width, int *height ) 
  7. {
  8.   /* This struct contains the JPEG decompression parameters and pointers to
  9.    * working space (which is allocated as needed by the JPEG library).
  10.    */
  11.   struct jpeg_decompress_struct cinfo;
  12.   /* We use our private extension JPEG error handler.
  13.    * Note that this struct must live as long as the main JPEG parameter
  14.    * struct, to avoid dangling-pointer problems.
  15.    */
  16.   /* This struct represents a JPEG error handler.  It is declared separately
  17.    * because applications often want to supply a specialized error handler
  18.    * (see the second half of this file for an example).  But here we just
  19.    * take the easy way out and use the standard error handler, which will
  20.    * print a message on stderr and call exit() if compression fails.
  21.    * Note that this struct must live as long as the main JPEG parameter
  22.    * struct, to avoid dangling-pointer problems.
  23.    */
  24.  
  25.   struct jpeg_error_mgr jerr;
  26.   /* More stuff */
  27.   JSAMPARRAY buffer;        /* Output row buffer */
  28.   int row_stride;        /* physical row width in output buffer */
  29.   unsigned char *out;
  30.   byte  *bbuf;
  31.   int nSize;
  32.  
  33.   /* Step 1: allocate and initialize JPEG decompression object */
  34.  
  35.   /* We have to set up the error handler first, in case the initialization
  36.    * step fails.  (Unlikely, but it could happen if you are out of memory.)
  37.    * This routine fills in the contents of struct jerr, and returns jerr's
  38.    * address which we place into the link field in cinfo.
  39.    */
  40.   cinfo.err = jpeg_std_error(&jerr);
  41.  
  42.   /* Now we can initialize the JPEG decompression object. */
  43.   jpeg_create_decompress(&cinfo);
  44.  
  45.   /* Step 2: specify data source (eg, a file) */
  46.  
  47.   jpeg_stdio_src(&cinfo, fbuffer);
  48.  
  49.   /* Step 3: read file parameters with jpeg_read_header() */
  50.  
  51.   (void) jpeg_read_header(&cinfo, TRUE);
  52.   /* We can ignore the return value from jpeg_read_header since
  53.    *   (a) suspension is not possible with the stdio data source, and
  54.    *   (b) we passed TRUE to reject a tables-only JPEG file as an error.
  55.    * See libjpeg.doc for more info.
  56.    */
  57.  
  58.   /* Step 4: set parameters for decompression */
  59.  
  60.   /* In this example, we don't need to change any of the defaults set by
  61.    * jpeg_read_header(), so we do nothing here.
  62.    */
  63.  
  64.   /* Step 5: Start decompressor */
  65.  
  66.   (void) jpeg_start_decompress(&cinfo);
  67.   /* We can ignore the return value since suspension is not possible
  68.    * with the stdio data source.
  69.    */
  70.  
  71.   /* We may need to do some setup of our own at this point before reading
  72.    * the data.  After jpeg_start_decompress() we have the correct scaled
  73.    * output image dimensions available, as well as the output colormap
  74.    * if we asked for color quantization.
  75.    * In this example, we need to make an output work buffer of the right size.
  76.    */ 
  77.   /* JSAMPLEs per row in output buffer */
  78.   row_stride = cinfo.output_width * cinfo.output_components;
  79.  
  80.   nSize = cinfo.output_width*cinfo.output_height*cinfo.output_components;
  81.   out = reinterpret_cast<unsigned char*>(malloc(nSize+1));
  82.   memset(out, 0, nSize+1);
  83.  
  84.   *pic = out;
  85.   *width = cinfo.output_width;
  86.   *height = cinfo.output_height;
  87.  
  88.   /* Step 6: while (scan lines remain to be read) */
  89.   /*           jpeg_read_scanlines(...); */
  90.  
  91.   /* Here we use the library's state variable cinfo.output_scanline as the
  92.    * loop counter, so that we don't have to keep track ourselves.
  93.    */
  94.   while (cinfo.output_scanline < cinfo.output_height) {
  95.     /* jpeg_read_scanlines expects an array of pointers to scanlines.
  96.      * Here the array is only one element long, but you could ask for
  97.      * more than one scanline at a time if that's more convenient.
  98.      */
  99.       bbuf = ((out+(row_stride*cinfo.output_scanline)));
  100.       buffer = &bbuf;
  101.     (void) jpeg_read_scanlines(&cinfo, buffer, 1);
  102.   }
  103.  
  104.   // clear all the alphas to 255
  105.   {
  106.       int    i, j;
  107.         byte    *buf;
  108.  
  109.         buf = *pic;
  110.  
  111.       j = cinfo.output_width * cinfo.output_height * 4;
  112.       for ( i = 3 ; i < j ; i+=4 ) {
  113.           buf[i] = 255;
  114.       }
  115.   }
  116.  
  117.   /* Step 7: Finish decompression */
  118.  
  119.   (void) jpeg_finish_decompress(&cinfo);
  120.   /* We can ignore the return value since suspension is not possible
  121.    * with the stdio data source.
  122.    */
  123.  
  124.   /* Step 8: Release JPEG decompression object */
  125.  
  126.   /* This is an important step since it will release a good deal of memory. */
  127.   jpeg_destroy_decompress(&cinfo);
  128.  
  129.   /* After finish_decompress, we can close the input file.
  130.    * Here we postpone it until after no more JPEG errors are possible,
  131.    * so as to simplify the setjmp error logic above.  (Actually, I don't
  132.    * think that jpeg_destroy can do an error exit, but why assume anything...)
  133.    */
  134.   //free (fbuffer);
  135.  
  136.   /* At this point you may want to check to see whether any corrupt-data
  137.    * warnings occurred (test whether jerr.pub.num_warnings is nonzero).
  138.    */
  139.  
  140.   /* And we're done! */
  141. }
  142.  
  143.